package cn.itcast.crm.dao;

import cn.itcast.crm.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface CustomerRepository extends JpaRepository<Customer,Integer> {
    /*
      @query 无法根据命名规则自动动态生成sql 的方法 nativequery true为sql语句 false为hql语句
      @modifying 表示该sql对数据库数据进行操作 如 增删改
      @transactional 事务管理 不开启事务管理的话 除了查询sql 其余sql 默认rollback
     */

    //查询所有fixedarea id为null
    public List<Customer> findByFixedAreaIdIsNull();
    //查询指定fixedarea id 的customer
    public List<Customer> findByFixedAreaId(String id);
     //按照id 更新customer 的fixedAreaId
    @Query("update Customer set fixedAreaId = ?1 where id = ?2 ")
    @Modifying
    public void updateFixedAreaId(String fixedAreaId, Integer id);

    @Query("update Customer set fixedAreaId = null where fixedAreaId = ?1")
    @Modifying
    void clearFixedAreaId(String fixedAreaId);

//    Customer findByTelephone(String telephone);
    public Customer findByTelephone(String telephone);
     @Query("update Customer set type = 1 where  telephone = ?1")
     @Modifying
     @Transactional
    void updateType(String telephone);
     //登录查询
    Customer findByTelephoneAndPassword(String telephone,String password);
    //根据address查询fixedareaId
    @Query("select fixedAreaId from Customer  where address = ?1")
    String findFixedAreaIdByAddress(String address);
}
